/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vastcm.stuhealth.client.framework.ui;

import com.vastcm.stuhealth.client.entity.BaseSetting;
import com.vastcm.stuhealth.client.entity.core.CoreEntity;
import com.vastcm.stuhealth.client.entity.service.core.ICoreService;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 * EditUI. Suitable as a superclass for UI with data object to persist.
 * @author house
 * @since  2013-04-13
 */
public abstract class EditUI<T extends CoreEntity> extends KernelUI 
    implements PropertyChangeListener {
    
    protected T data;
    private UIStatusEnum uiStatus;
    private PropertyChangeSupport propChangeSupport;

    /**
     * Creates new form EditUI
     */
    public EditUI() {
        initComponents();
    }

    @Override
    public void onLoad() throws Exception {
        super.onLoad(); 
        propChangeSupport = new PropertyChangeSupport(this);
        propChangeSupport.addPropertyChangeListener(this);
        initData();
    }
    
    protected void initData() throws Exception {
        try {
            uiStatus = ((UIStatusEnum) getUIContext().get(UIContext.UI_STATUS));
            if(data == null) {
                data = (T) getEntityClass().newInstance();
            }
            if(UIStatusEnum.EDIT.equals(uiStatus)) {
                String id = (String) getUIContext().get(UIContext.VO_ID);
                if(id != null) {
                    data = getService().getById(id);
                }
            }
            loadData();
            setUiStatus((UIStatusEnum) getUIContext().get(UIContext.UI_STATUS));
        } 
        catch (InstantiationException ex) {
            Logger.getLogger(EditUI.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IllegalAccessException ex) {
            Logger.getLogger(EditUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    /*
     * load data from EditUI's object value DATA to ui components.
     */
    public void loadData() throws Exception {};
    
    /*
     * store values of ui components to EditUI's object value DATA(class member T).
     */
    public void storeData() throws Exception {};
    
    public void save() throws Exception {
        storeData();
        getService().save(data);
        JOptionPane.showMessageDialog(this, "保存成功！");
    }
    
    public Class<?> getEntityClass() {
        Type genType = getClass().getGenericSuperclass();   
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();   
        return (Class<?>) params[0];  
    }
    
    public abstract ICoreService<T> getService();
    
    public UIStatusEnum getUiStatus() {
        return uiStatus;
    }

    public void setUiStatus(UIStatusEnum uiStatus) {
        UIStatusEnum oldStatus = UIStatusEnum.valueOf(uiStatus.name());
        if(oldStatus == uiStatus) {
            return;
        }
        this.uiStatus = uiStatus;
        PropertyChangeEvent changeEvent = new PropertyChangeEvent(this, "uiStatus", oldStatus, uiStatus);
        propChangeSupport.firePropertyChange(changeEvent);
    }
    
    /*
     * override this method to set ui component's status when editUI's status change to NEW.
     */
    public void onUiStatusChange2New() {
        
    }
    
    /*
     * override this method to set ui component's status when editUI's status change to EDIT.
     */
    public void onUiStatusChange2Edit() {
        
    }

    @Override
    public void propertyChange(PropertyChangeEvent pce) {
        if("uiStatus".equals(pce.getPropertyName())) {
            if(UIStatusEnum.NEW.equals(pce.getNewValue())) {
                onUiStatusChange2New();
            }
            if(UIStatusEnum.EDIT.equals(pce.getNewValue())) {
                onUiStatusChange2Edit();
            }
        }
    }
    
    

    /*
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
